home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / stlookup.c < prev    next >
C/C++ Source or Header  |  1990-04-12  |  3KB  |  78 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25. /* the builtins in this file manage symbol-table access.  they are
  26.    used by the predicates current_atom/1, current_functor/2 and
  27.    current_predicate/2.                            */
  28.  
  29. #include "builtin.h"
  30.  
  31. #define BINDREG(r, n)  op = reg[r]; DEREF(op); FOLLOW(op) = n; PUSHTRAIL(op)
  32.  
  33. b_STLOOKUP()
  34. {        /* reg 1: current bucket no.; reg. 2: current bucket element;
  35.            reg 3: element type sought; reg 4: 0 if this is a "new" search,
  36.            (i.e. if search should start at the beginning), 1 o/w; reg 5:
  37.            new bucket no.; reg 6: new element in bucket (ptr to ptr to
  38.            PSC table entry); reg 7: ptr to PSC table entry;        */
  39.  
  40.    PSC_REC_PTR psc_ptr;
  41.    LONG_PTR chain_ptr;
  42.    int i, elt_type;
  43.    LONG op;
  44.    register LONG_PTR top;
  45.  
  46.    op = reg[1]; DEREF(op);
  47.    i = INTVAL(op);
  48.    op = reg[4]; DEREF(op);
  49.    if (INTVAL(op)) {
  50.     op = reg[2]; DEREF(op);
  51.     chain_ptr = (LONG_PTR)UNTAGGED(op);
  52.     chain_ptr++;        /* ptr to next element */
  53.     }
  54.     else chain_ptr = (LONG_PTR)&hash_table[0][PERM];    /* init */
  55.     op = reg[3]; DEREF(op);
  56.     elt_type = INTVAL(op);
  57.     while (chain_ptr != NULL) {
  58.     if (!ISFREE(chain_ptr)) {
  59.         chain_ptr = (LONG_PTR)FOLLOW(chain_ptr);
  60.         psc_ptr = (PSC_REC_PTR)FOLLOW(chain_ptr);
  61.         if (GET_ETYPE(psc_ptr) == elt_type) {  /* found a match */
  62.         BINDREG(5, MAKEINT(i));
  63.         BINDREG(6, ((LONG)chain_ptr|CS_TAG));
  64.         BINDREG(7, ((LONG)psc_ptr | CS_TAG));
  65.         return;
  66.         }
  67.         else chain_ptr++;    /* didn't match, go to next element */
  68.     }
  69.     else {
  70.         i += 1;        /* next bucket */
  71.         if (i < BUCKET_CHAIN) chain_ptr = (LONG_PTR)&hash_table[i][PERM];
  72.         else chain_ptr = NULL;
  73.     } 
  74.     };
  75.     {FAIL0;}    /* can get here only if chain_ptr == NULL */
  76. }
  77.  
  78.